package com.link4a.doms.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.link4a.doms.Constants;
import com.link4a.doms.exception.BusinessException;
import com.link4a.doms.exception.ValidationException;
import com.link4a.doms.model.Order;
import com.link4a.doms.service.OrderService;
import com.link4a.doms.vo.OrderCondition;
import com.link4a.doms.web.ControllerSupport;
import com.link4a.doms.web.JsonResult;
import com.link4a.doms.web.auth.Privilege;

@Controller
@RequestMapping("order")
public class OrderController extends ControllerSupport{
	
	@Autowired
	private OrderService orderService;
	
	@RequestMapping
	@ResponseBody
	public JsonResult findPage(@RequestBody OrderCondition condition){
		return success(orderService.findPage(condition));
	}
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult delete(@PathVariable String id){
		try {
			orderService.delete(id);
			return success();
		} catch (BusinessException e) {
			return fail(e.getMessage());
		}
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult get(@PathVariable String id){
		return success(orderService.findById(id));
	}
	
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult create(@RequestBody Order order) throws BusinessException{
		if (orderService.findById(order.getId()) != null) {
			return result(ValidationException.DATA_DUPLICATED);
		} else {
			orderService.save(order);
			return success();
		}
	}
	
	@RequestMapping(method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult update(@RequestBody Order order){
		orderService.save(order);
		return success();
	}
	
	@RequestMapping(value="/deliver/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult deliver(@PathVariable String id){
		try {
			orderService.setDelivered(id);
			return success();
		} catch (BusinessException e) {
			return fail(e.getMessage());
		}
	}
	
	@RequestMapping(value="/sendEmail/{id}", method=RequestMethod.POST)
	@ResponseBody
	@Privilege(Constants.ORDER_CUD)
	public JsonResult sendEmail(@PathVariable String id){
		try {
			orderService.sendEmail(orderService.findById(id));
			return success();
		} catch (BusinessException e) {
			return fail(e.getMessage());
		}
	}
	
	@RequestMapping
	@Privilege(Constants.ORDER_INDEX)
	public ModelAndView index(@RequestParam(value = "cookie", required = false) Boolean cookie){
		ModelAndView mav = new ModelAndView("/business/order");
		mav.addObject("cookie", cookie);
		return mav;
	}
	
	@RequestMapping
	@Privilege(Constants.ORDER_CUD)
	public ModelAndView add(){
		ModelAndView mav = new ModelAndView("/business/orderForm");
		return mav;
	}
	
	@RequestMapping("/edit/{id}")
	@Privilege(Constants.ORDER_CUD)
	public ModelAndView edit(@PathVariable String id){
		ModelAndView mav = new ModelAndView("/business/orderForm");
		mav.addObject("id", id);
		return mav;
	}
}
